<?php

namespace App\user\controller;

use App\user\service\UserService;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use support\Request;

class UserController
{
    // 注册
    public function register(Request $request) {
        $type = $request->input('type', '');
        $param = [];
        switch ($type) {
            case 'mobile' :
                $param = $request->only(['mobile', 'code']);
                $res = UserService::validates($param, 'mobile_register');
                unset($param['code']);
                break;
            case 'email' :
                $param = $request->only(['email', 'password', 'againPassword']);
                $res = UserService::validates($param, 'email_register');
                $param['password'] = password_hash($param['password'], PASSWORD_DEFAULT);
                unset($param['againPassword']);
                break;
            default :
                return json(error('系统出错：注册失败！请联系客服：110！'));
                break;
        }
        if (!$res['code']) return json(error($res['msg']));

        $res = UserService::register($param);
        if ($res['code']) {
            return json(success());
        }
        return json(error($res['msg']));
    }

    // 登陆
    public function login(Request $request) {
        $type = $request->input('type', '');
        $param = [];
        switch ($type) {
            case 'mobile' :
                $param = $request->only(['mobile', 'code']);
                $res = UserService::validates($param, 'mobile_login');
                break;
            case 'email' :
                $param = $request->only(['email', 'password']);
                $res = UserService::validates($param, 'email_login');
                break;
            default :
                return json(error('系统出错：登陆失败！请联系客服：110！'));
                break;
        }
        if (!$res['code']) return json(error($res['msg']));
        $param['ip'] = $request->getRemoteIp();
        $res = UserService::login($param);
        if ($res['code']) {
            return json(success($res['data'], '登陆成功'));
        }
        return json(error($res['msg']));
    }

    // 退出
    public function logout(Request $request) {
        $param['token'] = $request->token;
        $res = UserService::logout($param);
        if ($res['code']) {
            return json(success());
        }
        return json(error($res['msg']));
    }

    // 注销账号
    public function writeOff(Request $request) {
        $param['token'] = $request->token;
        $res = UserService::writeOff($param);
        if ($res['code']) {
            return json(success());
        }
        return json(error($res['msg']));
    }

    // 修改个人信息
    public function editInfo(Request $request) {
        $param = $request->only([]);
        $param['token'] = $request->token;
        $res = UserService::login($param);
        if ($res['code']) {
            return json(success());
        }
        return json(error($res['msg']));
    }

    // 发送验证码
    public function sendCode(Request $request) {
        $param = $request->only(['mobile']);
        $validate = UserService::validates($param, 'send_code');
        if (!$validate['code']) return json(error($validate['msg']));
        $res = UserService::sendCode($param['mobile']);
        if ($res['code']) {
            return json(success($res['data']));
        }
        return json(error($res['msg']));
    }

    //  test 发送验证码
    public function add(Request $request) {
        $param = $request->only(['key', 'type']);
        $key = JWT::decode($param['key'], new Key('search_advert_create_four_step', 'HS256'));
        var_dump($key);
        return json(success(['asd' => 111, 'bbb' => 333], '123123123'));
    }
}